<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="企业信息" prop="companyInfo.id" :rules="[ ]">
              <GridSelect
                title="选择企业信息"
                labelName = 'companyName'
                labelValue = 'id'
                :value = "inputForm.companyInfo.id"
                :limit="1"
                @getValue='getValues(arguments)'
                :columns="[
                {
                  prop: 'companyName',
                  label: '企业名称'
                }
                ]"
                :searchs="[
                {
                  prop: 'companyName',
                  label: '企业名称'
                }
                ]"
                dataListUrl="/dlyrl/hr/enterpriseinformation/enterpriseInformation/list"
                entityBeanName="enterpriseInformation"
                queryEntityUrl="/dlyrl/hr/enterpriseinformation/enterpriseInformation/queryById">
              </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="企业用户" prop="companyUser.id" :rules="[ ]">
              <GridSelect
                title="选择企业用户"
                labelName = 'userName'
                labelValue = 'id'
                :disabled="inputForm.companyInfo.id==''"
                :value = "inputForm.companyUser.id"
                :limit="1"
                @getValue='(value) => {inputForm.companyUser.id=value}'
                :columns="[
                {
                  prop: 'userName',
                  label: '用户名'
                }
                ]"
                :searchs="[

                {
                  prop: 'userName',
                  label: '用户名'
                }
                ]"
                :dataListUrl=dataListUrl
                entityBeanName="enterpriseUserInformation"
                queryEntityUrl="/dlyrl/hr/enterpriseuserinformation/enterpriseUserInformation/queryById">
              </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="标题" prop="mettingTitle" :rules="[  ]">
              <el-input v-model="inputForm.mettingTitle" ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="内容" prop="mettingContent"
                :rules="[
                 ]">
          <!-- <el-input type="textarea" v-model="inputForm.mettingContent" placeholder="请填写内容"     ></el-input> -->
          <WangEditor ref="contractTemplateEditor" v-model="inputForm.mettingContent"/>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="会议时间" prop="mettingTime"
                :rules="[
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.mettingTime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="会议地址" prop="mettingAddress"
                :rules="[
                 ]">
              <el-input v-model="inputForm.mettingAddress"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="附件" prop="annex"
                :rules="[
                 ]">
              <el-upload ref="annex"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl/hr/mettingnotice/mettingnotice`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file) => {$window.open ((file.response && file.response.url) || file.url,'_blank') }"
                    :on-success="(response, file, fileList) => {
                       inputForm.annex = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.annex = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="annexArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  import WangEditor from '@/components/editor/WangEditor'
  export default {
    data () {
      return {
        title: '',
        method: '',
        companyName: '',
        userName: '',
        visible: false,
        loading: false,
        annexArra: [],
        inputForm: {
          id: '',
          mettingTitle: '',
          mettingContent: '',
          mettingTime: '',
          mettingAddress: '',
          companyInfo: {
            id: ''
          },
          companyUser: {
            id: ''
          },
          remarks: '',
          annex: ''
        },
        dataListUrl:'',//查创建人接口
      }
    },
    components: {
      GridSelect,
      WangEditor,
    },
    methods: {
      getValues(value){
        // console.log(value,'value')
        this.inputForm.companyInfo.id=value[0];
        this.dataListUrl='/dlyrl/hr/enterpriseuserinformation/enterpriseUserInformation/list?enterpriseInformation='+this.inputForm.companyInfo.id
      },
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建会议通知`
        } else if (method === 'edit') {
          this.title = '会议通知'
        } else if (method === 'view') {
          this.title = '查看会议通知'
        }
        this.annexArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.$refs.contractTemplateEditor.init('')
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/mettingnotice/mettingnotice/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.mettingnotice)
              this.$refs.contractTemplateEditor.init(this.inputForm.mettingContent)
              this.inputForm.annex.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.annexArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/mettingnotice/mettingnotice/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>